﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZP.Common;
using ZP.DTO.Common;
using ZP.DTO.ep_honor_title;
using ZP.DTO.ht_type1_title;
using ZP.IBLL;

namespace ZPJWeb.Controllers
{
    public class EPHonorController : BaseController
    {
        //public EPHonorController(Iht_honor_titleService _ht_honor_titleService,
        //  Iht_type1_titleService _ht_type1_titleService
        //, Iht_type1_fileService _ht_type1_fileService)
        //{
        //    this._ht_honor_titleService = _ht_honor_titleService;
        //    this._ht_type1_titleService = _ht_type1_titleService;
        //    this._ht_type1_fileService = _ht_type1_fileService;
        //}

        /// <summary>
        /// 获得所有荣誉信息及附件类型
        /// </summary>
        /// <returns></returns>
        public JsonResult GetEPHonorList()
        {
            ResultData<List<EPHonorResultDTO>> resultData = new ResultData<List<EPHonorResultDTO>>();
            try
            {
                //根据登录信息获得ep_id
                PortalUserLoginDTO loginUser = (PortalUserLoginDTO)Session[ZPCommon.SESSION_Portal_Login];
                if (Session[ZPCommon.SESSION_Portal_Login] != null)
                {
                    int ep_id = loginUser.user_id;
                    resultData.Object = _ht_honor_titleService.GetEPHonorList(ep_id);
                    resultData.SubmitResult = true;
                }
            }
            catch (Exception e)
            {
                resultData.Exception = e;
                resultData.Message = e.Message;
            }
            return Json(resultData, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 上传荣誉附件页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadTitleFile(int ht_id = 0)
        {
            ViewBag.ht_id = ht_id;
            return View();
        }

        public JsonResult GetTitleFile(int ht_id = 0)
        {
            ResultData<List<EPTitleFileResultDTO>> resultData = new ResultData<List<EPTitleFileResultDTO>>();
            try
            {
                //根据登录信息获得ep_id
                if (Session[ZPCommon.SESSION_Portal_Login] != null)
                {
                    PortalUserLoginDTO loginUser = (PortalUserLoginDTO)Session[ZPCommon.SESSION_Portal_Login];
                    int ep_id = loginUser.user_id;
                    resultData.Object = _ht_honor_titleService.GetTitleFile(ep_id, ht_id);
                    resultData.SubmitResult = true;
                }
            }
            catch (Exception e)
            {
                resultData.Exception = e;
                resultData.Message = e.Message;
            }
            return Json(resultData, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 批量上传-附件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadEPAnnex(int ht_id, int ft_id)
        {
            FileResultData result = new FileResultData { SaveMode = FileSaveModeEnum.File };

            var oFile = Request.Files[0];
            //var type = Request.Form["type"];
            string errorMsg = "";
            if (oFile == null)
            {
                errorMsg = "没有找到文件！";
            }
            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                result.Message = errorMsg;
            }

            //UserLoginDTO loginUser = (UserLoginDTO)Session[ZPCommon.SESSION_Login];
            try
            {
                int ep_id = 0;
                if (Session[ZPCommon.SESSION_Portal_Login] != null)
                {
                    PortalUserLoginDTO loginUser = (PortalUserLoginDTO)Session[ZPCommon.SESSION_Portal_Login];
                    ep_id = loginUser.user_id;
                }
                if(ep_id == 0)
                {
                    return Json(result);
                }

                string ep_ht_ft = string.Format("{0}/{1}/{2}", ep_id, ht_id, ft_id);    //根据企业ID、荣誉ID、标题ID存放文件
                string filepath = SaveAsUploadFile(oFile, ep_ht_ft);
                //将上传后的附件地址保存对应企业荣誉标题下
                result.FileId = _ht_type1_fileService.SaveEPFile(ep_id, ht_id, ft_id, oFile.FileName, filepath).ToString();
                result.SubmitResult = true;

                result.FilePath = filepath;
                result.FileName = oFile.FileName;
                result.Extension = System.IO.Path.GetExtension(result.FileName); //得到扩展名
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 删除企业对应荣誉标题的附件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteEPFile(Guid? file_id)
        {
            ResultData<string> result = new ResultData<string>();

            if (!file_id.HasValue || file_id.Value == Guid.Empty)
            {
                result.Message = "附件删除失败!错误码:" + ErrorCode.Error_1000;
            }
            else
            {
                string fl_phy_add = "";
                try
                {
                    if (Session[ZPCommon.SESSION_Portal_Login] != null)
                    {
                        PortalUserLoginDTO loginUser = (PortalUserLoginDTO)Session[ZPCommon.SESSION_Portal_Login];
                        int ep_id = loginUser.user_id;
                        result.SubmitResult = _ht_type1_fileService.DeleteEPFile(ep_id, file_id.Value, out fl_phy_add);
                    }
                }
                catch (Exception ex)
                {
                    result.Message = ex.Message;
                }
            }
            return Json(result);
        }
    }
}